﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client;
using PPI.Client.Interface;
using PPI.Client.Presenter;

namespace PPI
{
    public partial class frmSplashStartup : frmBase, ISplashStartup
    {
        private SplashStartupPresenter m_Presenter;

        public frmSplashStartup()
        {
            InitializeComponent();
            base.MenuMain.Visible = false;
            m_Presenter = new SplashStartupPresenter(this);
        }

        public void updateProgressBar(int value)
        {
            this.pbLoading.Value = value;
        }

        public void CloseApplication(string message)
        {
            DevComponents.DotNetBar.MessageBoxEx.Show(message, "Pole Position Desktop", MessageBoxButtons.OK);
            this.Close();
            Application.ExitThread();
        }

        private void frmSplashStartup_Load(object sender, EventArgs e)
        {
            this.m_BackgroundWorkerLoad.RunWorkerAsync();
        }

        protected override void LoadDoWork(object sender, DoWorkEventArgs e)
        {
            UpdateMessage("Checking ...");
            //m_Presenter.CheckIsInstanceAlreadyRunning();
            m_Presenter.CheckInternetConnection();
            UpdateMessage("Loading Data ...");
            m_Presenter.LoadCacheData();
        }

        protected override void LoadCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }

        private delegate void UpdateMessageCallback(string message);

        public void UpdateMessage(string message)
        {
            if (this.InvokeRequired)
            {
                UpdateMessageCallback d = new UpdateMessageCallback(UpdateMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                lblMessageProgress.Text = message;
            }
        }


    }
}
